//
//  MicroMovieViewController.m
//  觅影
//
//  Created by Zeus on 16/4/8.
//  Copyright © 2016年  陶虹. All rights reserved.
//

#import "MicroMovieViewController.h"
#import "MicroMainModel.h"
#import "MicroMovieTableViewCell.h"
#import "MicroDetailViewController.h"

@interface MicroMovieViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UISegmentedControl *segment;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic, strong)NSMutableArray *modelArray;

@end

@implementation MicroMovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"微影";
    self.navigationController.navigationBar.translucent = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"MicroMovieTableViewCell" bundle:nil] forCellReuseIdentifier:@"MicroMovieCell"];
    self.tableView.rowHeight = KWIDTH * 777/1242;
    [self.segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    [self getData:@"http://baobab.wandoujia.com/api/v1/ranklist?strategy=weekly"];
    
        
}

#pragma mark --- 请求数据 ---
- (void)getData:(NSString *)urlString
{
    [RequestManager requestWithUrl:urlString requestType:RequestTypeGET parDic:nil finshed:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *videoListArray = dic[@"videoList"];
        self.modelArray = [NSMutableArray array];
        for (NSDictionary *videoListDic in videoListArray) {
            MicroMainModel *model = [[MicroMainModel alloc] init];
            [model setValuesForKeysWithDictionary:videoListDic];
            [self.modelArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });

    } error:^(NSError *error) {
        NSLog(@"请求错误");
    }];
}

#pragma mark --- segment点击事件 ---
- (void)segmentAction:(UISegmentedControl *)segment
{
    if (self.segment.selectedSegmentIndex == 0) {
        [self getData:@"http://baobab.wandoujia.com/api/v1/ranklist?strategy=weekly"];
    }
    else
    {
        [self getData:@"http://baobab.wandoujia.com/api/v1/ranklist?strategy=monthly"];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MicroMovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MicroMovieCell" forIndexPath:indexPath];
    MicroMainModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    cell.numberLabel.text = [NSString stringWithFormat:@"%d",indexPath.row + 1];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MicroDetailViewController *detailVC = [[MicroDetailViewController alloc]init];
    MicroMainModel *model = self.modelArray[indexPath.row];
    detailVC.model = model;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;

}


@end



